// pages/regist/regist.js

Page({
  data: {
    userName: '',
    password: '',
    confirmPassword: ''
  },

  // 输入绑定
  inputUserName(e) {
    this.setData({ userName: e.detail.value.trim() });
  },
  inputPassword(e) {
    this.setData({ password: e.detail.value.trim() });
  },
  inputConfirmPassword(e) {
    this.setData({ confirmPassword: e.detail.value.trim() });
  },

  // 注册请求
  async onRegister() {
    const { userName, password, confirmPassword } = this.data;
    
    // 输入验证
    if (!userName || !password) {
      wx.showToast({ title: '用户名和密码不能为空', icon: 'none' });
      return;
    }
    if (password !== confirmPassword) {
      wx.showToast({ title: '两次密码输入不一致', icon: 'none' });
      return;
    }

    try {
      wx.request({
        url: 'https://pszap.top/register',
        method:'POST',
        data:{
          "user_name": userName,
          "password": password
        },
        success: () => {
          wx.navigateTo({ url: '/pages/login/login' });
        },
        fail: () => {
          wx.showToast({ title: res.data.msg || '注册失败', icon: 'none' });
        }
      })
    } catch (error) {
      console.error('注册失败:', error);
      wx.showToast({ title: '网络请求失败', icon: 'none' });
    }
  }
});